#ifndef STREAMBUFFER_H
#define STREAMBUFFER_H

#include <QIODevice>
#include <QFile>
#include <QMutex>

/**
 * Provides a file-backed random-access buffer device, with functions
 * to fill the buffer without disturbing the read.
 */
class StreamBuffer : public QIODevice
{
public:
    StreamBuffer(const QString& streamName, int size);
    ~StreamBuffer();
    
public: // from QIODevice
    bool atEnd () const;
    qint64 bytesAvailable () const;
    qint64 bytesToWrite () const;
    bool canReadLine () const;
    void close ();
//    bool isSequential () const;
    bool open ( OpenMode mode );
    qint64 pos () const;
    bool reset ();
    bool seek ( qint64 pos );
    qint64 size () const;
//    bool waitForBytesWritten ( int msecs );
//    bool waitForReadyRead ( int msecs );
    qint64 readData(char* data, qint64 maxSize);
//    qint64 readLineData ( char * data, qint64 maxSize );
    qint64 writeData(const char* data, qint64 maxSize);

public slots:
    void timerEvent(QTimerEvent* event);

private:
    QFile cacheFile;
    qint64 contentLength;
    qint64 readPos;
    qint64 writePos;
    mutable QMutex lock;
};

#endif // STREAMBUFFER_H
